package com.helei.strategy;

import com.helei.entity.root.MessageGroup;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @ Author：hl
 * @ Date：2024-07-10-上午11:09
 */
@Service
public class StrategyContext {
    //当前对应的策略采用lambda的方法存储
    Map<String, Function<MessageGroup, String>> strategyContextMap = new HashMap<>();

    @Resource
    CalcPriceService calcPriceService;

    @PostConstruct
    @SneakyThrows
    public void setStrategyMap() {
        //群里
        strategyContextMap.put("group", (calcPriceParam) -> calcPriceService.group(calcPriceParam));
        strategyContextMap.put("private", (calcPriceParam) -> calcPriceService.individual(calcPriceParam));
    }

    public void calcPrice(MessageGroup messageGroup) {
        //获取对应的方法计算并返回
        Function<MessageGroup, String> calcPriceFunc = strategyContextMap.get(messageGroup.getMessageType());
        calcPriceFunc.apply(messageGroup);

    }
}
